package org.tio.sitexxx.im.client.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.sitexxx.im.common.Command;
import org.tio.sitexxx.im.common.CommandHandler;
import org.tio.sitexxx.im.common.ImPacket;
import org.tio.sitexxx.im.common.bs.JoinGroupResp;
import org.tio.sitexxx.service.vo.Const.JoinGroupResult;
import org.tio.utils.hutool.StrUtil;
import org.tio.utils.json.Json;

/**
 * 进群响应处理者
 * @author tanyaowu
 * 2016年5月9日 上午11:46:24
 */
@CommandHandler(Command.JOIN_GROUP_RESP)
public class JoinGroupRespHandler implements ImClientHandler {
	private static Logger log = LoggerFactory.getLogger(JoinGroupRespHandler.class);

	public JoinGroupRespHandler() {

	}

	@Override
	public void handler(ImPacket packet, ChannelContext channelContext) throws Exception {
		//		ImSessionContext imSessionContext = (ImSessionContext) channelContext.getAttribute(Const.IM_SESSION_KEY);

		JoinGroupResp joinGroupResp = Json.toBean(packet.getBodyStr(), JoinGroupResp.class);
		String g = joinGroupResp.getG();
		if (StrUtil.isBlank(g)) {
			log.error("{}, groupid is null", channelContext);
			return;
		}
		Byte resultValue = joinGroupResp.getResult();
		JoinGroupResult result = JoinGroupResult.from(resultValue);
		if (result == JoinGroupResult.OK) {
			Tio.bindGroup(channelContext, g + "");
		} else {
			log.warn("{}, 进群失败, groupid:{}, 失败原因:{}", channelContext, g, result);
			return;
		}
	}
}
